﻿
Imports std

Public Class x_separated_names
    Inherits iParseable

    Private _v As vector(Of String) = Nothing

    Protected Sub New(ByVal v As vector(Of String))
        _v = v
    End Sub

    Public Shared Operator +(ByVal this As x_separated_names) As vector(Of String)
        If Not this Is Nothing Then
            Return this._v
        Else
            Return Nothing
        End If
    End Operator

    Default Public ReadOnly Property at(ByVal index As Int64) As String
        Get
            If _v Is Nothing Then
                Return Nothing
            Else
                Return _v(index)
            End If
        End Get
    End Property

    Public Function size() As Int64
        If _v Is Nothing Then
            Return 0
        Else
            Return _v.size()
        End If
    End Function

    Public Delegate Function createDelegate(Of T)(ByVal v As vector(Of String)) As T
    Public Delegate Function name_defination_parse_delegate(ByVal input As String, ByRef index As Int64, _
                                                            ByRef output As String) As Boolean
    Protected Shared Function parseImpl(Of T)(ByVal input As String, ByRef index As Int64, ByRef output As T, _
                                              ByVal separator As charcheck, _
                                              ByVal parse As name_defination_parse_delegate, _
                                              ByVal creator As createDelegate(Of T)) As Boolean
        assert(Not separator Is Nothing, "separator is nothing.")
        assert(Not parse Is Nothing, "parse is nothing")
        assert(Not creator Is Nothing, "creator is nothing.")

        Dim v As vector(Of String) = Nothing
        v = New vector(Of String)()
        Dim s As String = Nothing
        If parse(input, index, s) Then
            v.push_back(s)
            While token(input, index, Nothing, separator, AddressOf [false])
                If value_name_defination.parse(input, index, s) Then
                    v.push_back(s)
                Else
                    Return False
                End If
            End While
        End If

        output = creator(v)
        Return True
    End Function

    Public Overloads Function ToString(ByVal separator As String) As String
        If _v Is Nothing Then
            Return Nothing
        Else
            Return _v.ToString(separator)
        End If
    End Function

    Public Overrides Function ToString() As String
        Return ToString(character.comma)
    End Function
End Class
